﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model;
using HZ.Supervise.IBLL;
using System.Collections.Generic;

public partial class Company_CompanyInfo_PS : BasePage
{
    public Company_CompanyInfo_PS()
    { 
       
    }

    protected void Page_Load(object sender, EventArgs e) 
    {
        QueryControl1.OnQueryClick += new EventHandler(QueryControl1_OnQueryClick);
        if (!IsPostBack)
        {
            ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
            QueryControl1.BindDataPre((SuperviseQuery)ViewState["query"]);//绑定查询条件的一些数据源 质量层次等
            BindData();//绑定数据
            QueryControl1.BindSort(gvwCompanyInfo);
        }
    }

    void QueryControl1_OnQueryClick(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        ViewState["query"] = QueryControl1.UpdateQueryFromValue((SuperviseQuery)ViewState["query"]);
        BindData();
    }

    private void BindData()
    {
        int t = 0;
         
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        { 
            GroupField = GroupByField.CompanyName_TB, 
            Query = (SuperviseQuery)ViewState["query"],
            GroupSelectField = GroupFields.PurchaseAmountTotal | GroupFields.DistributeAmountTotal | GroupFields.DistributeAmountTotal | GroupFields.DistributeRate | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseRate,
            SortField = QueryControl1.SortField==""?"CompanyName_TB ASC":QueryControl1.SortField
        };
 
        System.Data.DataTable dt = ie.ExecutePage(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
        AspNetPager1.RecordCount = t;
        //显示相应的字段
        GridViewUtil.AdjustGridView(gvwCompanyInfo, dt);

        gvwCompanyInfo.DataSource = dt;
        gvwCompanyInfo.DataBind();
        setChart(dt);
        SetColumnVisible();
    }
     

    

    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }
     

    protected void gvwCompanyInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //按配送企业查看
        if (e.CommandName == "DirectByCom")
        {
            //设置分组查询字段GroupSelectField
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.PurchaseCountTotal | GroupFields.PurchaseAmountTotal | GroupFields.WareHouseCountTotal;
            }
             
            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.CompanyName_TB = e.CommandArgument.ToString();
            Session[sessionid] = query;
            string url =System.Configuration.ConfigurationManager.AppSettings["CompanyID_PS"];
            Response.Redirect(string.Format("{0}?sessionid={1}",url, sessionid));

        }

        //按医院查看
        if (e.CommandName == "DirectByHos")
        {
            //设置分组查询字段GroupSelectField
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.PurchaseCountTotal | GroupFields.PurchaseAmountTotal | GroupFields.WareHouseCountTotal;
            }
            
            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.CompanyName_TB = e.CommandArgument.ToString();
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings["HospitalID"];
            Response.Redirect(string.Format("{0}?sessionid={1}",url, sessionid));
        }

        //按药品查看
        if (e.CommandName == "DierectByDrug")
        {
            //设置分组查询字段GroupSelectField
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.PurchaseCountTotal | GroupFields.PurchaseAmountTotal | GroupFields.WareHouseCountTotal;
            }
            
 
            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.CompanyName_TB = e.CommandArgument.ToString();
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings["ProcureCatalogID"];
            Response.Redirect(string.Format("{0}?sessionid={1}",url, sessionid));
        }

        //查看详细
        if (e.CommandName == "DierectByDetail")
        {
            //设置分组查询字段GroupSelectField
            if (ShowColumns.ShowColumn == null)
            {
                ShowColumns.ShowColumn = new ShowColumns();
                ShowColumns.ShowColumn.GroupFields = GroupFields.DistributeCountTotal | GroupFields.DistributeRate | GroupFields.PurchaseCountTotal | GroupFields.WareHouseCountTotal;
            }
            
            string sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            query.CompanyName_TB = e.CommandArgument.ToString();
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings["HosComDrugs"];
            Response.Redirect(string.Format("{0}?sessionid={1}",url, sessionid)); 

        }
    }

     
     
    /// <summary>
    /// 屏蔽相关查询连接
    /// </summary>
    private void SetColumnVisible()
    {
        if (!string.IsNullOrEmpty(QueryControl1.ProcureCatalogID))
        {
            foreach (GridViewRow gvr in gvwCompanyInfo.Rows)
            {
                gvr.FindControl("lbnDrug").Visible = false;
            }
        }
        if (!string.IsNullOrEmpty(QueryControl1.CompanyID_PS))
        {
            foreach (GridViewRow gvr in gvwCompanyInfo.Rows)
            {
                gvr.FindControl("lbnCom_PS").Visible = false;
            }
        }
        if (!string.IsNullOrEmpty(QueryControl1.HospitalID))
        {
            foreach (GridViewRow gvr in gvwCompanyInfo.Rows)
            {
                gvr.FindControl("lbnHos").Visible = false;
            }
        }
    }

    protected void ddlOther_SelectedIndexChanged(object sender, EventArgs e)
    {
        string stkey = "";
        string sessionid = string.Empty;
        GridViewRow row = (GridViewRow)((Control)sender).Parent.Parent;
        DropDownList ddl = (DropDownList)row.FindControl("ddlOther");
        stkey = ddl.SelectedValue;
        if (stkey != "")
        {
            sessionid = Guid.NewGuid().ToString();
            SuperviseQuery query = (SuperviseQuery)ViewState["query"];
            //赋予当前行的主键值
            //query.CompanyName_TB = row.Cells[1].Text; //((LinkButton)row.FindControl("lbnHos")).CommandArgument;
            query.CompanyName_TB = gvwCompanyInfo.DataKeys[row.RowIndex].Value.ToString();
            
            Session[sessionid] = query;
            string url = System.Configuration.ConfigurationManager.AppSettings[stkey];
            Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
        }
    }

    /// <summary>
    /// 设置图表
    /// </summary>
    /// <param name="?"></param>
    private void setChart(System.Data.DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            this.ChartControl1.Visible = false;
            string[] _columns = ShowColumns.ShowColumn.GroupFields.ToString().Split(',');
            HZ.Supervise.Web.WebService.ChartConfig serChart = new HZ.Supervise.Web.WebService.ChartConfig();
            dt.TableName = "中标企业";
            serChart.DataSource = dt;
            serChart.XName = "CompanyName_TB";
            serChart.YNames = _columns[0];
            serChart.ChartTypes = HZ.Supervise.Web.WebService.ChartType.PieChart;

            ChartControl1.webChart = serChart;
            ChartControl1.PageId = Guid.NewGuid().ToString();
            ChartControl1.EntranceChart();
        }
        else
        {
            ChartControl1.Visible = false;
        }
    }
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        int t = 0;

        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            GroupField = GroupByField.CompanyName_TB,
            Query = (SuperviseQuery)ViewState["query"],
            GroupSelectField = (ShowColumns.ShowColumn == null) ? GroupFields.DistributeCountTotal | GroupFields.PurchaseCountTotal | GroupFields.PurchaseAmountTotal | GroupFields.WareHouseCountTotal : ShowColumns.ShowColumn.GroupFields,
            SortField = QueryControl1.SortField == "" ? "CompanyName_TB ASC" : QueryControl1.SortField
        };

        System.Data.DataTable dt = ie.ExecutePage(0,0, out t); 
        //显示相应的字段
         //GridViewUtil.AdjustGridView(gvwCompanyInfo, dt);  
        ExportFile.Instance.ExportData(dt, gvwCompanyInfo, this.Page);
    }
    protected void lbtnSee_Click(object sender, EventArgs e)
    {
        if (this.lbtnSee.Text == "显示图表")
        {
            ChartControl1.Visible = true;
            this.lbtnSee.Text = "隐藏图表";
        }
        else
        {
            ChartControl1.Visible = false;
            this.lbtnSee.Text = "显示图表";
        }
    }
}
